package com.obtk.shilipai.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 自定义拦截器的配置，实现WebMvcConfigurer不会导致Spring Boot对mvc的自动配置失效
 */
@Configuration
public class LoginInterceptorConfig implements WebMvcConfigurer {
//    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 实现WebMvcConfigurer不会导致静态资源被拦截
        registry.addInterceptor(new LoginInterceptor())
                // 拦截所有url
                .addPathPatterns("/**")
                // 放行swagger
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/api/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/shop/**")
                .excludePathPatterns("/dishe/**")
                //放行登录页面
                .excludePathPatterns("/login")
                .excludePathPatterns("/selectUser")
                .excludePathPatterns("/setShopInfo")
                .excludePathPatterns("/getShopType")
                .excludePathPatterns("/updateShop")
                .excludePathPatterns("/addShop")
                .excludePathPatterns("/reg")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/selectShopName")
                //所有静态页面默认映射到static目录下
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/loginShop");
    }

    /**
     * 添加静态资源文件，外部可以直接访问地址
     *
     * @param registry
     */
//    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //需要配置1：----------- 需要告知系统，这是要被当成静态文件的！
        //第一个方法设置访问路径前缀，第二个方法设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}